/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.fzhucc.modules.expert.service;

import com.fzhucc.exception.BadRequestException;
import com.fzhucc.modules.expert.domain.Expert;
import com.fzhucc.modules.expert.domain.ExpertTag;
import com.fzhucc.modules.expert.domain.dto.ExpertTagDto;
import com.fzhucc.modules.expert.domain.dto.ExpertTagQueryCriteria;
import com.fzhucc.modules.expert.repository.ExpertRepository;
import com.fzhucc.modules.expert.repository.ExpertTagRepository;
import com.fzhucc.modules.expert.service.mapstruct.ExpertTagMapper;
import com.fzhucc.modules.system.service.DictDetailService;
import com.fzhucc.modules.system.service.DictService;
import com.fzhucc.modules.system.service.dto.DictDetailDto;
import com.fzhucc.modules.system.service.dto.DictDto;
import com.fzhucc.modules.system.service.dto.DictQueryCriteria;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import com.fzhucc.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * @author yyg
 * @website https://eladmin.vip
 * @description 服务实现
 * @date 2023-03-10
 **/
@Service
@RequiredArgsConstructor
public class ExpertTagService {

    private final ExpertTagRepository expertTagRepository;

    private final ExpertRepository expertRepository;
    private final ExpertTagMapper expertTagMapper;

    private final DictService dictService;
    private final DictDetailService dictDetailService;

    public Map<String, Object> queryAll(ExpertTagQueryCriteria criteria, Pageable pageable) {
        Page<ExpertTag> page = expertTagRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        Page<ExpertTagDto> map = page.map(expertTagMapper::toDto);
        List<ExpertTagDto> list = map.getContent();
        if (list != null) {
            for (ExpertTagDto expertTagDto : list) {
                Optional<Expert> res = expertRepository.findById(expertTagDto.getExpertId());
                if (res.isPresent()) {
                    expertTagDto.setExpert(res.get());
                }
            }
        }
        return PageUtil.toPage(map);
    }

    public List<ExpertTagDto> queryAll(ExpertTagQueryCriteria criteria) {
        return expertTagMapper.toDto(expertTagRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    public ExpertTagDto findById(Integer id) {
        ExpertTag expertTag = expertTagRepository.findById(id).orElseGet(ExpertTag::new);
        ValidationUtil.isNull(expertTag.getId(), "ExpertTag", "id", id);
        return expertTagMapper.toDto(expertTag);
    }

    @Transactional(rollbackFor = Exception.class)
    public ExpertTagDto create(ExpertTag resources) {
        Optional<Expert> res = expertRepository.findById(resources.getExpertId());
        if (!res.isPresent()) {
            throw new BadRequestException("未找到该专家");
        }
        return expertTagMapper.toDto(expertTagRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(ExpertTag resources) {
        Optional<Expert> res = expertRepository.findById(resources.getExpertId());
        if (!res.isPresent()) {
            throw new BadRequestException("未找到该专家");
        }
        ExpertTag expertTag = expertTagRepository.findById(resources.getId()).orElseGet(ExpertTag::new);
        ValidationUtil.isNull(expertTag.getId(), "ExpertTag", "id", resources.getId());
        expertTag.copy(resources);
        expertTagRepository.save(expertTag);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            expertTagRepository.deleteById(id);
        }
    }

    public void download(List<ExpertTagDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ExpertTagDto expertTag : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("专家id", expertTag.getExpertId());
            map.put("标签分类     expert_label_type", expertTag.getType());
            map.put("标签具体值   business_expertise_label_type、familiar_specialty_label_type", expertTag.getLabel());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    public List<DictDetailDto> getExpertLabelType() {
        DictQueryCriteria dictQueryCriteria = new DictQueryCriteria();
        dictQueryCriteria.setBlurry("expert_label_type");
        List<DictDto> list = dictService.queryAll(dictQueryCriteria);
        if (list != null && list.size() == 1) {
            List<DictDetailDto> list1 = dictDetailService.getDictByDictId(list.get(0).getId());
            return list1;
        }
        return null;
    }
    public List<DictDetailDto> getBusinessExpertiseLabelType() {
        DictQueryCriteria dictQueryCriteria = new DictQueryCriteria();
        dictQueryCriteria.setBlurry("business_expertise_label_type");
        List<DictDto> list = dictService.queryAll(dictQueryCriteria);
        if (list != null && list.size() == 1) {
            List<DictDetailDto> list1 = dictDetailService.getDictByDictId(list.get(0).getId());
            return list1;
        }
        return null;
    }

    public List<DictDetailDto> getFamiliarSpecialtyLabelType() {
        DictQueryCriteria dictQueryCriteria = new DictQueryCriteria();
        dictQueryCriteria.setBlurry("familiar_specialty_label_type");
        List<DictDto> list = dictService.queryAll(dictQueryCriteria);
        if (list != null && list.size() == 1) {
            List<DictDetailDto> list1 = dictDetailService.getDictByDictId(list.get(0).getId());
            return list1;
        }
        return null;
    }
}